package com.chinasofti.manager.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.chinasofti.dao.EmpRepository;
import com.chinasofti.dao.OccupationApplicationRepository;
import com.chinasofti.dao.PerformanceRepository;
import com.chinasofti.manager.service.AdminApplyService;
import com.chinasofti.model.Emp;
import com.chinasofti.model.OccupationApplication;
import com.chinasofti.model.Performance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Creator: <br>
 * Date: <br>
 * Description: <br>
 */
@Service
@Component
public class AdminApplyServiceImpl implements AdminApplyService{
    @Autowired
    private EmpRepository empRepository;
    @Autowired
    private OccupationApplicationRepository occupationApplicationRepository;
    @Autowired
    private PerformanceRepository performanceRepository;

    @Override
    public List<OccupationApplication> queryApplyList() {
        return occupationApplicationRepository.findAll();
    }

    @Override
    public void operate(Long id, Integer result) {
        OccupationApplication occupationApplication = occupationApplicationRepository.findByIdEquals(id);
        occupationApplication.setState(result);
        occupationApplicationRepository.save(occupationApplication);
        Emp emp = empRepository.findByName(occupationApplication.getName());
        emp.setDeptName(occupationApplication.getTargetDept());
        empRepository.save(emp);
        if (result == 1){
            //请假批准，扣分
            Performance performance = performanceRepository.findByName(emp.getName());
            performance.setTotalPoints(performance.getTotalPoints() - 1);
            performanceRepository.save(performance);
        }
    }

}
